package com.abvnet.project.water.sewage.service;

import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.common.utils.text.Convert;
import com.abvnet.project.water.project.mapper.ProjectInfoMapper;
import com.abvnet.project.water.project.mapper.ProjectMicrobialMapper;
import com.abvnet.project.water.sewage.domain.SewagePlan;
import com.abvnet.project.water.sewage.domain.SewagePlanMicrobial;
import com.abvnet.project.water.sewage.domain.SewagePlanProject;
import com.abvnet.project.water.sewage.mapper.SewagePlanMapper;
import com.abvnet.project.water.sewage.mapper.SewagePlanMicrobialMapper;
import com.abvnet.project.water.sewage.mapper.SewagePlanProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 解决方案 服务层实现
 * 
 * @author duj
 * @date 2019-07-30
 */
@Service
public class SewagePlanServiceImpl implements ISewagePlanService {
	@Autowired
	private SewagePlanMapper sewagePlanMapper;

	@Autowired
    private SewagePlanMicrobialMapper sewagePlanMicrobialMapper;

    @Autowired
    private SewagePlanProjectMapper sewagePlanProjectMapper;

    @Autowired
    private ProjectMicrobialMapper projectMicrobialMapper;

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Override
    public int cancelAssignProject(SewagePlanProject sewagePlanProject) {
        return sewagePlanProjectMapper.cancelAssignProject(sewagePlanProject);
    }

    @Override
    public int cancelAssignProjectAll(Long planId, String projectIds) {
        return sewagePlanProjectMapper.cancelAssignProjectAll(planId, Convert.toLongArray(projectIds));
    }

    @Override
    public int insertAssingProjects(Long planId, String projectIds) {
        return sewagePlanProjectMapper.batchPlanProjects(Arrays.stream(Convert.toLongArray(projectIds)).map(p -> SewagePlanProject.builder().projectId(p).planId(planId).build()).collect(Collectors.toList()));
    }

    /**
     * 查询解决方案信息
     * 
     * @param planId 解决方案ID
     * @return 解决方案信息
     */
    @Override
	public SewagePlan selectSewagePlanById(Long planId) {
	    return sewagePlanMapper.selectSewagePlanById(planId);
	}

    @Override
    public int cancelAssignMicro(SewagePlanMicrobial sewagePlanMicrobial) {
        return sewagePlanMicrobialMapper.cancelAssignMicro(sewagePlanMicrobial);
    }

    @Override
    public int cancelAssignMicroAll(Long planId, String microbialIds) {
        return sewagePlanMicrobialMapper.cancelAssignMicroAll(planId, Convert.toLongArray(microbialIds));
    }

    @Override
    public int insertAssingMicros(Long planId, String microbialIds) {
        return sewagePlanMicrobialMapper.batchPlanMicrobial(Arrays.stream(Convert.toLongArray(microbialIds)).map(m -> SewagePlanMicrobial.builder().microbialId(m).planId(planId).build()).collect(Collectors.toList()));
    }

    /**
     * 查询解决方案列表
     * 
     * @param sewagePlan 解决方案信息
     * @return 解决方案集合
     */
	@Override
	public List<SewagePlan> selectSewagePlanList(SewagePlan sewagePlan) {
	    return sewagePlanMapper.selectSewagePlanList(sewagePlan);
	}

    @Override
    public List<SewagePlan> selectSewagePlanListForApi(SewagePlan sewagePlan) {
        return sewagePlanMapper.selectSewagePlanListForApi(sewagePlan);
    }

    @Override
    public SewagePlan selectSewagePlanForApi(Long planId) {
        SewagePlan sp = sewagePlanMapper.selectSewagePlanForApi(planId);
        sp.setProjectMicrobialList(projectMicrobialMapper.selectMicrobialListByPlanId(planId));
        sp.setProjectInfoList(projectInfoMapper.selectProjectListByPlanId(planId));
        return sp;
    }

    /**
     * 新增解决方案
     * 
     * @param sewagePlan 解决方案信息
     * @return 结果
     */
	@Override
	public int insertSewagePlan(SewagePlan sewagePlan) {
        sewagePlan.setCreateBy(ShiroUtils.getLoginName());
	    return sewagePlanMapper.insertSewagePlan(sewagePlan);
	}
	
	/**
     * 修改解决方案
     * 
     * @param sewagePlan 解决方案信息
     * @return 结果
     */
	@Override
	public int updateSewagePlan(SewagePlan sewagePlan) {
        sewagePlan.setUpdateBy(ShiroUtils.getLoginName());
	    return sewagePlanMapper.updateSewagePlan(sewagePlan);
	}

	/**
     * 删除解决方案对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteSewagePlanByIds(String ids) {
		return sewagePlanMapper.deleteSewagePlanByIds(Convert.toStrArray(ids));
	}
	
}